package org.noote.roby.core;

import org.noote.libs.computervision.buffer.Buffer8888XRGB;
import  org.noote.libs.computervision.detection.line.LineDetection_MergeSegments;

import android.graphics.Color;

public class LineDetection extends LineDetection_MergeSegments {

	Buffer8888XRGB _input = new Buffer8888XRGB();
	Buffer8888XRGB _output = new Buffer8888XRGB();

	@Override
	public boolean createBuffer(int width, int height)
	{
		if(_width != width || _height != height)
		{
			if(!super.createBuffer(width, height)) return false;
			
			_input.createBuffer(width, height);
			_output.createBuffer(width, height);
		}
		
	    return true;
	}
	
	public void detect() {
		detect(_input);
	}

	public void copy_from(int[] buffer, int width, int height) {
		_input.createFrom(width, height, buffer);
	}

	public void print_black()
	{
		_output.fill(Color.BLACK);
	}

	public void show_ransac_grids()
	{
		for(int x=1; x<(_width/_ransac_block); x++)
			drawVerticalLineDot(x*_ransac_block, 0, _height, Color.LTGRAY);
		for(int y=1; y<(_height/_ransac_block); y++)
			drawHorizontalLineDot(0, y*_ransac_block, _width, Color.LTGRAY);
	}

	public void print_input()
	{
		_output.copyFrom(_input);
	}

	public void print_intensity()
	{
		exportTo(_output);
	}

	public void print_sobel()
	{
		exportEdgeTo(_output);
	}

	public void show_edges()
	{
		for(int fh=1; fh<_height-1; fh++)
		{
			int hb = fh*_width;
			for(int fw=1; fw<_width-1; fw++)
			{
				int i=hb+fw;
			
				if(_iEdge[i]>_sobel_limit_min)
				{
					_output.putPixel(i, Color.YELLOW);
				}
			}
		}
	}

	public void show_orientation()
	{
		for(int fh=1; fh<_height-1; fh++)
		{
			int hb = fh*_width;
			for(int fw=1; fw<_width-1; fw++)
			{
				int i=hb+fw;
			
				if(_iEdge[i]>_sobel_limit_min)
				{
					if(_fOrientation[i]>=-(Math.PI/4.0f) && _fOrientation[i]<=(Math.PI/4.0f))
						_output.putPixel(i, Color.YELLOW); // up
					else if(_fOrientation[i]>(Math.PI/4.0f) && _fOrientation[i]<(3*Math.PI/4.0f))
						_output.putPixel(i, Color.BLUE); // right
					else if(_fOrientation[i]<-(Math.PI/4.0f) && _fOrientation[i]>-(3*Math.PI/4.0f))
						_output.putPixel(i, Color.RED); //left
					else // if(_fOrientation[i]>Math.PI) 
						_output.putPixel(i, Color.GREEN); // down
				}
			}
		}
	}
	public void show_ransac_edgels()
	{
		for(Edgel edgel : _allransacedgels)
		{
			int c = Color.LTGRAY;
			if(edgel.orientation>=(-Math.PI*0.25f) && edgel.orientation<=(Math.PI*0.25f))
				c = Color.YELLOW; // up
			else if(edgel.orientation>(Math.PI*0.25f) && edgel.orientation<=(Math.PI*0.75f))
				c = Color.BLUE; // right
			else if(edgel.orientation<(-Math.PI*0.25f) && edgel.orientation>=(-Math.PI*0.75f))
				c = Color.RED; //left
			else // if(edgel.orientation>Math.PI) 
				c = Color.MAGENTA; // down
			drawCircle(edgel.position, 1, c);
		}
	}
	public void show_ransac_segments()
	{
		for(OrientedSegment line : _allransacsegments)
		{
			int c = Color.LTGRAY;
			if(line.orientation>=(-Math.PI*0.25f) && line.orientation<=(Math.PI*0.25f))
				c = Color.YELLOW; // up
			else if(line.orientation>(Math.PI*0.25f) && line.orientation<=(Math.PI*0.75f))
				c = Color.BLUE; // right
			else if(line.orientation<(-Math.PI*0.25f) && line.orientation>=(-Math.PI*0.75f))
				c = Color.RED; //left
			else // if(line.orientation>Math.PI) 
				c = Color.MAGENTA; // down

			drawCircle(line.start, 2, c);
			drawCircle(line.end, 1, c);
			drawLine(line.start, line.end, c);
		}
	}
	public void show_segments()
	{
		for(Segment line : _allmergedsegments)
		{
			int c = Color.WHITE;
			drawCircle(line.start, 2, c);
			drawCircle(line.end, 1, c);
			drawLine(line.start, line.end, c);
		}
	}
}
